package cn.edu.sdjzu.xg.eagle.dao;

import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class CommonDao {
    private static CommonDao commonDao = new CommonDao();
    private static Collection<Teacher> teachers; // 未使用的字段，可根据需求保留或删除

    private CommonDao() {
        // 私有构造方法，确保单例模式
    }

    public static CommonDao getInstance() {
        return commonDao;
    }

    /**
     * 返回指定表的元素数量
     *
     * @param connection    数据库连接对象
     * @param tableName     表名
     * @param conditionList
     * @return 元组数量
     * @throws SQLException
     */
    public int count(Connection connection, String tableName, List<Condition> conditionList) throws SQLException {
        String sql_count = "SELECT COUNT(id) as cnt FROM " + tableName;
        PreparedStatement pstmt_count = connection.prepareStatement(sql_count);
        int counter = 0;
        ResultSet resultSet_count = pstmt_count.executeQuery();
        if (resultSet_count.next()) {
            counter = resultSet_count.getInt("cnt");
        }
        JdbcHelper.close(resultSet_count, pstmt_count);
        return counter;
    }
}
